home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Codigo / hh / rsource.exe / Heretic Source / I_SOUND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-08  |  8.2 KB  |  392 lines

  1.  
  2. // I_SOUND.C
  3.  
  4. #include <stdio.h>
  5. #include "doomdef.h"
  6. #include "dmx.h"
  7. #include "sounds.h"
  8. #include "i_sound.h"
  9.  
  10. /*
  11. ===============
  12. =
  13. = I_StartupTimer
  14. =
  15. ===============
  16. */
  17.  
  18. int tsm_ID = -1;
  19.  
  20. void I_StartupTimer (void)
  21. {
  22. #ifndef NOTIMER
  23.     extern int I_TimerISR(void);
  24.  
  25.     tprintf("I_StartupTimer()\n",0);
  26.     // installs master timer.  Must be done before StartupTimer()!
  27.     TSM_Install(SND_TICRATE);
  28.     tsm_ID = TSM_NewService (I_TimerISR, 35, 255, 0); // max priority
  29.     if (tsm_ID == -1)
  30.     {
  31.         I_Error("Can't register 35 Hz timer w/ DMX library");
  32.     }
  33. #endif
  34. }
  35.  
  36. void I_ShutdownTimer (void)
  37. {
  38.     TSM_DelService(tsm_ID);
  39.     TSM_Remove();
  40. }
  41.  
  42. /*
  43.  *
  44.  *                           SOUND HEADER & DATA
  45.  *
  46.  *
  47.  */
  48.  
  49. // sound information
  50. #if 0
  51. const char *dnames[] = {"None",
  52.             "PC_Speaker",
  53.             "Adlib",
  54.             "Sound_Blaster",
  55.             "ProAudio_Spectrum16",
  56.             "Gravis_Ultrasound",
  57.             "MPU",
  58.             "AWE32"
  59.             };
  60. #endif
  61.  
  62. const char snd_prefixen[] = { 'P', 'P', 'A', 'S', 'S', 'S', 'M',
  63.   'M', 'M', 'S' };
  64.  
  65. int snd_Channels;
  66. int snd_DesiredMusicDevice, snd_DesiredSfxDevice;
  67. int snd_MusicDevice,    // current music card # (index to dmxCodes)
  68.     snd_SfxDevice,      // current sfx card # (index to dmxCodes)
  69.     snd_MaxVolume,      // maximum volume for sound
  70.     snd_MusicVolume;    // maximum volume for music
  71. int dmxCodes[NUM_SCARDS]; // the dmx code for a given card
  72.  
  73. int     snd_SBport, snd_SBirq, snd_SBdma;       // sound blaster variables
  74. int     snd_Mport;                              // midi variables
  75.  
  76. extern boolean  snd_MusicAvail, // whether music is available
  77.         snd_SfxAvail;   // whether sfx are available
  78.  
  79. void I_PauseSong(int handle)
  80. {
  81.   MUS_PauseSong(handle);
  82. }
  83.  
  84. void I_ResumeSong(int handle)
  85. {
  86.   MUS_ResumeSong(handle);
  87. }
  88.  
  89. void I_SetMusicVolume(int volume)
  90. {
  91.   MUS_SetMasterVolume(volume*8);
  92. //  snd_MusicVolume = volume;
  93. }
  94.  
  95. void I_SetSfxVolume(int volume)
  96. {
  97.   snd_MaxVolume = volume; // THROW AWAY?
  98. }
  99.  
  100. /*
  101.  *
  102.  *                              SONG API
  103.  *
  104.  */
  105.  
  106. int I_RegisterSong(void *data)
  107. {
  108.   int rc = MUS_RegisterSong(data);
  109. #ifdef SNDDEBUG
  110.   if (rc<0) printf("MUS_Reg() returned %d\n", rc);
  111. #endif
  112.   return rc;
  113. }
  114.  
  115. void I_UnRegisterSong(int handle)
  116. {
  117.   int rc = MUS_UnregisterSong(handle);
  118. #ifdef SNDDEBUG
  119.   if (rc < 0) printf("MUS_Unreg() returned %d\n", rc);
  120. #endif
  121. }
  122.  
  123. int I_QrySongPlaying(int handle)
  124. {
  125.   int rc = MUS_QrySongPlaying(handle);
  126. #ifdef SNDDEBUG
  127.   if (rc < 0) printf("MUS_QrySP() returned %d\n", rc);
  128. #endif
  129.   return rc;
  130. }
  131.  
  132. // Stops a song.  MUST be called before I_UnregisterSong().
  133.  
  134. void I_StopSong(int handle)
  135. {
  136.   int rc;
  137.   rc = MUS_StopSong(handle);
  138. #ifdef SNDDEBUG
  139.   if (rc < 0) printf("MUS_StopSong() returned %d\n", rc);
  140. #endif
  141.   // Fucking kluge pause
  142.   {
  143.     int s;
  144.     extern volatile int ticcount;
  145.     for (s=ticcount ; ticcount - s < 10 ; );
  146.   }
  147. }
  148.  
  149. void I_PlaySong(int handle, boolean looping)
  150. {
  151.   int rc;
  152.   rc = MUS_ChainSong(handle, looping ? handle : -1);
  153. #ifdef SNDDEBUG
  154.   if (rc < 0) printf("MUS_ChainSong() returned %d\n", rc);
  155. #endif
  156.   rc = MUS_PlaySong(handle, snd_MusicVolume);
  157. #ifdef SNDDEBUG
  158.   if (rc < 0) printf("MUS_PlaySong() returned %d\n", rc);
  159. #endif
  160.  
  161. }
  162.  
  163. /*
  164.  *
  165.  *                                 SOUND FX API
  166.  *
  167.  */
  168.  
  169. // Gets lump nums of the named sound.  Returns pointer which will be
  170. // passed to I_StartSound() when you want to start an SFX.  Must be
  171. // sure to pass this to UngetSoundEffect() so that they can be
  172. // freed!
  173.  
  174.  
  175. int I_GetSfxLumpNum(sfxinfo_t *sound)
  176. {
  177.   char namebuf[9];
  178.  
  179.   if(sound->name == 0)
  180.         return 0;
  181.   if (sound->link) sound = sound->link;
  182. //  sprintf(namebuf, "d%c%s", snd_prefixen[snd_SfxDevice], sound->name);
  183.   return W_GetNumForName(sound->name);
  184.  
  185. }
  186.  
  187. int I_StartSound (int id, void *data, int vol, int sep, int pitch, int priority)
  188. {
  189. /*
  190.   // hacks out certain PC sounds
  191.   if (snd_SfxDevice == PC
  192.     && (data == S_sfx[sfx_posact].data
  193.     ||  data == S_sfx[sfx_bgact].data
  194.     ||  data == S_sfx[sfx_dmact].data
  195.     ||  data == S_sfx[sfx_dmpain].data
  196.     ||  data == S_sfx[sfx_popain].data
  197.     ||  data == S_sfx[sfx_sawidl].data)) return -1;
  198.  
  199.   else
  200.         */
  201.     return SFX_PlayPatch(data, pitch, sep, vol, 0, 0);
  202.  
  203. }
  204.  
  205. void I_StopSound(int handle)
  206. {
  207. //  extern volatile long gDmaCount;
  208. //  long waittocount;
  209.   SFX_StopPatch(handle);
  210. //  waittocount = gDmaCount + 2;
  211. //  while (gDmaCount < waittocount) ;
  212. }
  213.  
  214. int I_SoundIsPlaying(int handle)
  215. {
  216.   return SFX_Playing(handle);
  217. }
  218.  
  219. void I_UpdateSoundParams(int handle, int vol, int sep, int pitch)
  220. {
  221.   SFX_SetOrigin(handle, pitch, sep, vol);
  222. }
  223.  
  224. /*
  225.  *
  226.  *                                                      SOUND STARTUP STUFF
  227.  *
  228.  *
  229.  */
  230.  
  231. //
  232. // Why PC's Suck, Reason #8712
  233. //
  234.  
  235. void I_sndArbitrateCards(void)
  236. {
  237.     char tmp[160];
  238.   boolean gus, adlib, pc, sb, midi;
  239.   int i, rc, mputype, p, opltype, wait, dmxlump;
  240.  
  241. //  snd_MaxVolume = 127;
  242.  //Damn you, Dave Taylor!
  243.  
  244.   snd_MusicDevice = snd_DesiredMusicDevice;
  245.   snd_SfxDevice = snd_DesiredSfxDevice;
  246.  
  247.   // check command-line parameters- overrides config file
  248.   //
  249.   if (M_CheckParm("-nosound")) snd_MusicDevice = snd_SfxDevice = snd_none;
  250.   if (M_CheckParm("-nosfx")) snd_SfxDevice = snd_none;
  251.   if (M_CheckParm("-nomusic")) snd_MusicDevice = snd_none;
  252.  
  253.   if (snd_MusicDevice > snd_MPU && snd_MusicDevice <= snd_MPU3)
  254.     snd_MusicDevice = snd_MPU;
  255.   if (snd_MusicDevice == snd_SB)
  256.     snd_MusicDevice = snd_Adlib;
  257.   if (snd_MusicDevice == snd_PAS)
  258.     snd_MusicDevice = snd_Adlib;
  259.  
  260.   // figure out what i've got to initialize
  261.   //
  262.   gus = snd_MusicDevice == snd_GUS || snd_SfxDevice == snd_GUS;
  263.   sb = snd_SfxDevice == snd_SB || snd_MusicDevice == snd_SB;
  264.   adlib = snd_MusicDevice == snd_Adlib ;
  265.   pc = snd_SfxDevice == snd_PC;
  266.   midi = snd_MusicDevice == snd_MPU;
  267.  
  268.   // initialize whatever i've got
  269.   //
  270.   if (gus)
  271.   {
  272.     if (GF1_Detect()) tprintf("Dude.  The GUS ain't responding.\n",1);
  273.     else
  274.     {
  275.       dmxlump = W_GetNumForName("dmxgus");
  276.       GF1_SetMap(W_CacheLumpNum(dmxlump, PU_CACHE), lumpinfo[dmxlump].size);
  277.     }
  278.  
  279.   }
  280.   if (sb)
  281.   {
  282.     if(debugmode)
  283.     {
  284.         sprintf(tmp,"cfg p=0x%x, i=%d, d=%d\n",
  285.       snd_SBport, snd_SBirq, snd_SBdma);
  286.       tprintf(tmp,0);
  287.     }
  288.     if (SB_Detect(&snd_SBport, &snd_SBirq, &snd_SBdma, 0))
  289.     {
  290.       sprintf(tmp,"SB isn't responding at p=0x%x, i=%d, d=%d\n",
  291.       snd_SBport, snd_SBirq, snd_SBdma);
  292.       tprintf(tmp,0);
  293.     }
  294.     else SB_SetCard(snd_SBport, snd_SBirq, snd_SBdma);
  295.  
  296.     if(debugmode)
  297.     {
  298.         sprintf(tmp,"SB_Detect returned p=0x%x,i=%d,d=%d\n",
  299.       snd_SBport, snd_SBirq, snd_SBdma);
  300.       tprintf(tmp,0);
  301.     }
  302.   }
  303.  
  304.   if (adlib)
  305.   {
  306.     if (AL_Detect(&wait,0))
  307.       tprintf("Dude.  The Adlib isn't responding.\n",1);
  308.     else
  309.         AL_SetCard(wait, W_CacheLumpName("genmidi", PU_STATIC));
  310.   }
  311.  
  312.   if (midi)
  313.   {
  314.     if (debugmode)
  315.     {
  316.         sprintf(tmp,"cfg p=0x%x\n", snd_Mport);
  317.         tprintf(tmp,0);
  318.     }
  319.  
  320.     if (MPU_Detect(&snd_Mport, &i))
  321.     {
  322.       sprintf(tmp,"The MPU-401 isn't reponding @ p=0x%x.\n", snd_Mport);
  323.       tprintf(tmp,0);
  324.     }
  325.     else MPU_SetCard(snd_Mport);
  326.   }
  327.  
  328. }
  329.  
  330. // inits all sound stuff
  331.  
  332. void I_StartupSound (void)
  333. {
  334.     char tmp[80];
  335.   int rc, i;
  336.  
  337.   if (debugmode)
  338.     tprintf("I_StartupSound: Hope you hear a pop.\n",1);
  339.  
  340.   // initialize dmxCodes[]
  341.   dmxCodes[0] = 0;
  342.   dmxCodes[snd_PC] = AHW_PC_SPEAKER;
  343.   dmxCodes[snd_Adlib] = AHW_ADLIB;
  344.   dmxCodes[snd_SB] = AHW_SOUND_BLASTER;
  345.   dmxCodes[snd_PAS] = AHW_MEDIA_VISION;
  346.   dmxCodes[snd_GUS] = AHW_ULTRA_SOUND;
  347.   dmxCodes[snd_MPU] = AHW_MPU_401;
  348.   dmxCodes[snd_AWE] = AHW_AWE32;
  349.  
  350.   // inits sound library timer stuff
  351.   I_StartupTimer();
  352.  
  353.   // pick the sound cards i'm going to use
  354.   //
  355.   I_sndArbitrateCards();
  356.  
  357.   if (debugmode)
  358.   {
  359.     sprintf(tmp,"  Music device #%d & dmxCode=%d", snd_MusicDevice,
  360.       dmxCodes[snd_MusicDevice]);
  361.     tprintf(tmp,0);
  362.     sprintf(tmp,"  Sfx device #%d & dmxCode=%d\n", snd_SfxDevice,
  363.       dmxCodes[snd_SfxDevice]);
  364.     tprintf(tmp,0);
  365.   }
  366.  
  367.   // inits DMX sound library
  368.   tprintf("  calling DMX_Init",0);
  369.   rc = DMX_Init(SND_TICRATE, SND_MAXSONGS, dmxCodes[snd_MusicDevice],
  370.     dmxCodes[snd_SfxDevice]);
  371.  
  372.   if (debugmode)
  373.   {
  374.     sprintf(tmp,"  DMX_Init() returned %d", rc);
  375.     tprintf(tmp,0);
  376.   }
  377.  
  378. }
  379.  
  380. // shuts down all sound stuff
  381.  
  382. void I_ShutdownSound (void)
  383. {
  384.   DMX_DeInit();
  385.   I_ShutdownTimer();
  386. }
  387.  
  388. void I_SetChannels(int channels)
  389. {
  390.   WAV_PlayMode(channels, SND_SAMPLERATE);
  391. }
  392.